<?php

namespace app\api\controller;

use think\Db;

//小店
class Shop extends BaseController
{
    //我的小店
    public function getShopInfo()
    {
        $shop_id = input('post.shop_id', '');
        if (!$shop_id || 24 != strlen($shop_id)) {
            return error('参数错误：错误的shop_id');
        }
        $user_info = session('user_info');

        $data = Db::name('shop')->find($shop_id);
        if ($data['user_id'] == $user_info['id']) {
            $data['shopkeeper'] = 1;
        }
        if ($data['gang_id'] == $user_info['gang_id']) {
            $data['is_member'] = 1;
        }
        $shopkeeper = Db::name('user')->field('id,name,avatar')->find($data['user_id']);
        $data['shopkeeper'] = $shopkeeper;
        $data['config_tags'] = config('tags');

        return success($data);
    }

    // 获取小店产品列表
    public function getShopProduct()
    {
        $user_info = session('user_info');
        $shop_id = input('post.shop_id', '');
        if (!$shop_id || 24 != strlen($shop_id)) {
            return error('参数错误：错误的shop_id');
        }
        $type = input('post.type', '');
        $brand = input('post.brand', '');
        $query = ['sold_out' => 0];
        if ($type) {
            $query['type'] = $type;
        }
        if ($brand) {
            $query['brand'] = $brand;
        }
        $shop_info = Db::name('shop')->find($shop_id);
        $list = Db::name('shop_product')->where('shop_id', $shop_id)->select();
        $data = [];
        foreach ($list as $key => $value) {
            $query['id'] = $value['product_id'];
            $info = Db::name('product')->field('id,name,pic,price,discount,specifications,tag_price')->where($query)->find();
            if (!$info) {
                continue;
            }
            $info['shop_product_id'] = $value['id'];
            $min = 9999999999;
            $gang_price_list = Db::name('gang_product')->where(['product_id' => $info['id'], 'gang_id' => $shop_info['gang_id'], 'sold_out' => 0])->select();
            if (!$gang_price_list) {
                continue;
            }
            foreach ($gang_price_list as $k => $v) {
                if (!$v['gang_price']) {
                    continue;
                }
                if ($min > $v['gang_price']) {
                    $min = round($v['gang_price'], 2);
                }
            }
            foreach ($info['specifications'] as $k => $v) {
                $info['specifications'][$k] = [];
                $info['specifications'][$k]['title'] = $v['title'];
                $info['specifications'][$k]['stock'] = $v['stock'];
                foreach ($gang_price_list as $kk => $gang_price) {
                    if ($gang_price['specifications'] == $v['title']) {
                        //会员显示帮会价格 游客显示零售价格
                        if ($user_info['gang_id'] == $shop_info['gang_id']) {
                            $info['specifications'][$k]['gang_price'] = round($gang_price['gang_price'], 2);
                            $info['specifications'][$k]['retail_price'] = round($gang_price['retail_price'], 2);
                            $info['specifications'][$k]['tag_price'] = round($info['suggest_price'], 2);
                        } else {
                            $info['specifications'][$k]['gang_price'] = round($gang_price['retail_price'], 2);
                            $info['specifications'][$k]['retail_price'] = round($v['suggest_price'], 2);
                            $info['specifications'][$k]['tag_price'] = round($info['tag_price'], 2);
                        }
                    }
                }
            }
            $info['price'] = round($min, 2);
            $info['discount'] = round($min / $info['tag_price'] * 10, 1);
            $data[] = $info;
        }

        return success($data);
    }

    // 获取小店未添加的产品列表
    public function getShopSoldOutProduct()
    {
        $shop_id = input('post.shop_id', '');
        if (!$shop_id || 24 != strlen($shop_id)) {
            return error('参数错误：错误的shop_id');
        }
        $user_info = session('user_info');
        $type = input('post.type', '');
        $brand = input('post.brand', '');
        if ($type) {
            $query['type'] = $type;
        }
        if ($brand) {
            $query['brand'] = $brand;
        }
        $shop_info = Db::name('shop')->find($shop_id);
        $arr = Db::name('shop_product')->where(['shop_id' => $shop_id])->field('product_id')->select();
        $ids = [];
        foreach ($arr as $key => $value) {
            $ids[] = $value['product_id'];
        }
        $list = Db::name('product')->where(['classify' => 0, 'sold_out' => 0, 'id' => ['nin', $ids]])->select();
        $data = [];
        foreach ($list as $key => $info) {
            $min = 9999999999;
            $gang_price_list = Db::name('gang_product')->where(['product_id' => $info['id'], 'gang_id' => $shop_info['gang_id']])->select();
            foreach ($gang_price_list as $k => $v) {
                if (!$v['gang_price']) {
                    continue;
                }
                if ($min > $v['gang_price']) {
                    $min = round($v['gang_price'], 2);
                }
            }
            $row = [];
            $row['id'] = strval($info['id']);
            $row['pic'] = strval($info['pic']);
            $row['name'] = strval($info['name']);
            $row['price'] = round($min, 2);
            $row['discount'] = round($min / $info['tag_price'] * 10, 2);
            $data[] = $row;
        }

        return success($data);
    }

    //添加产品
    public function addProduct()
    {
        $shop_id = input('post.shop_id', '');
        if (!$shop_id || 24 != strlen($shop_id)) {
            return error('参数错误：错误的shop_id');
        }
        $product_ids = input('post.product_ids/a', '');
        if (!$product_ids || 0 == count($product_ids)) {
            return error('参数错误：错误的product_ids');
        }
        $user_info = session('user_info');
        $list = [];
        foreach ($product_ids as $key => $product_id) {
            $p = Db::name('shop_product')->where(['product_id' => $product_id, 'user_id' => $user_info['id']])->find();
            if ($p) {
                continue;
            }
            $data = [];
            $data['user_id'] = $user_info['id'];
            $data['gang_id'] = $user_info['gang_id'];
            $data['shop_id'] = $shop_id;
            $data['product_id'] = $product_id;
            $list[] = $data;
        }
        Db::name('shop_product')->insertAll($list);

        return success();
    }

    //删除产品
    public function delProduct()
    {
        $shop_product_id = input('post.shop_product_id', '');
        if (!$shop_product_id || 24 != strlen($shop_product_id)) {
            return error('参数错误：错误的shop_product_id');
        }
        $data = Db::name('shop_product')->find($shop_product_id);
        if (!$data) {
            return error('未找到该产品');
        }
        $user_info = session('user_info');
        if ($data['user_id'] != $user_info['id']) {
            return error('权限不足');
        }
        $data = Db::name('shop_product')->delete($shop_product_id);

        return success($data);
    }

    // 获取小店评价的tags
    public function getTags()
    {
        $user_id = input('post.user_id', '');
        if (!$user_id || 24 != strlen($user_id)) {
            return error('参数错误：错误的user_id');
        }
        $tags = Db::name('shop')->where(['user_id' => $user_id])->find();

        return success(['tags' => $tags['tags'], 'config_tags' => config('tags')]);
    }

    //查看小店评价列表
    public function getEvaluateList()
    {
        $user_id = input('post.user_id', '');
        if (!$user_id || 24 != strlen($user_id)) {
            return error('参数错误：错误的user_id');
        }
        // tags -1全部 0带图 1+其他tags
        $tags = input('post.tags', -1);
        if ($tags != -1) {
            if (0 == $tags) {
                $query['pics'] = ['neq', null];
            } else {
                $query['tags'] = intval($tags);
            }
        }
        $query['seller'] = $user_id;
        $limit = input('post.limit', 10);
        $page = input('post.page', 1);
        $order = ['ct' => -1];
        $data = Db::name('evaluate')->where($query)->order($order)->limit($limit)->page($page)->select();
        foreach ($data as $key => $value) {
            $data[$key]['ct'] = date('Y-m-d', $value['ct']);
            $buyer = Db::name('user')->field('id,name,avatar')->find($value['buyer']);
            $data[$key]['buyer'] = $buyer;
        }

        return success(['data' => $data, 'is_over' => count($data) < $limit]);
    }
}
